package com.hk.Mgr.web.controller.app;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.functors.ForClosure;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hk.Mgr.web.message.StatusCode;
import com.hk.Mgr.web.message.kyc.UserExtralReq;
import com.hk.Mgr.web.message.kyc.UserExtralRes;
import com.hk.base.BaseCtrl;
import com.hk.base.ResponseBase;
import com.hk.commons.ConstantPointRule;
import com.hk.commons.HqlFilter;
import com.hk.commons.ReqToModelHelper;
import com.hk.commons.StringHelper;
import com.hk.commons.entity.UserExtraInfo;
import com.hk.commons.entity.sys.SysUser;
import com.hk.commons.vo.app.UserPointsMsg;
import com.hk.service.IPointsFunction;
import com.hk.service.IUserExtraInfo;
import com.hk.service.kyc.IActivity;
import com.hk.service.sys.ISysUser;

/**
 * 用户积分
 * 
 * @author jeff
 *
 */
@Controller
@RequestMapping(value = BaseCtrl.App + "/userPointsCtr")
public class AppUserPointsCtrl extends BaseCtrl {

	@Autowired
	private ISysUser userService;

	@Autowired
	private IActivity activityService;

	@Autowired
	private IUserExtraInfo userExtraService;

	@Autowired
	private IPointsFunction PointsFunctionService;

	/*
	 * 获取用户的当前积分
	 * 
	 * @author jeff
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getUserPointsByUserId", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase<UserExtralRes> getUserPointsByUserId(
			@RequestBody UserExtralReq req) {
		ResponseBase<UserExtralRes> resp = new ResponseBase<UserExtralRes>();

		Long userId = req.getUserId();

		logger.info("app/userPointsCtr/getUserPointsByUserId userId = "
				+ userId);

		if (userService.existById(userId)) {
			UserExtraInfo userInfo = userExtraService.findByUserId(userId);
			if (userInfo == null) {
				userInfo = new UserExtraInfo();
			}
			UserExtralRes resInfo = new UserExtralRes();
			ReqToModelHelper.copyReqValueToModel(userInfo, resInfo);
			SysUser user = userService.findById(userInfo.getUserId());
			if (user != null) {
				resInfo.setUserRealName(user.getEncryptRealName());
				resInfo.setUserPhoneNum(user.getEncryptName());
			}
			resInfo.setLeftPoints(userInfo.getLeftPoints());
			resp.setSize(1);
			resp.setResult(resInfo);
			resp.setResultCode(StatusCode.SUCCESS.key());
		} else {
			resp.setMsg("改用户不存在！");
			resp.setResultCode(StatusCode.PARAM_ERROR.key());
		}
		return resp;
	}

	/*
	 * 根据输入的用户信息获取内容
	 * 
	 * @author jeff
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getListByUserInfo", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase<List<UserExtralRes>> getListByUserInfo(
			@RequestBody UserExtralReq req) {
		ResponseBase<List<UserExtralRes>> resp = new ResponseBase<List<UserExtralRes>>();

		List<UserExtralRes> respList = new ArrayList<UserExtralRes>();

		List<UserExtraInfo> infoList = new ArrayList<UserExtraInfo>();

		HqlFilter hqlFilter = new HqlFilter();
		Boolean filetrUser = false;
		String userRealName = req.getUserRealName();
		String userPhoneNum = req.getUserPhoneNum();

		Integer pageSize = req.getPageSize();
		Integer curPage = req.getCurPage();

		Integer sortType = req.getSortType();
		logger.info("app/userPointsCtr/getListBySortInfo userRealName = "
				+ userRealName + "  userPhoneNum = " + userPhoneNum
				+ "   curPage = " + curPage + "   pageSize = " + pageSize);

		if (StringHelper.isNotEmpty(userRealName)) {
			filetrUser = true;
			hqlFilter.addLKFilter("realName", userRealName);
		}

		if (StringHelper.isNotEmpty(userPhoneNum)) {
			filetrUser = true;
			hqlFilter.addLKFilter("name", userPhoneNum);
		}

		// 如果用户的筛选条件不为空，则筛选用户信息
		if (filetrUser) {
			infoList = getListByUserName(hqlFilter, curPage, pageSize);

			if (infoList != null) {
				for (UserExtraInfo extraInfo : infoList) {
					UserExtralRes resInfo = new UserExtralRes();
					ReqToModelHelper.copyReqValueToModel(extraInfo, resInfo);
					SysUser user = userService.findById(extraInfo.getUserId());
					if (user != null) {
						resInfo.setUserRealName(user.getEncryptRealName());
						resInfo.setUserPhoneNum(user.getEncryptName());
					}
					respList.add(resInfo);
				}
			}

			resp.setResult(respList);
			resp.setSize(respList.size());
			resp.setResultCode(StatusCode.SUCCESS.key());
		} else {
			resp.setMsg("用户信息不存在！");
			resp.setResultCode(StatusCode.PARAM_ERROR.key());
		}

		return resp;
	}

	/*
	 * 根据输入的用户信息获取内容
	 * 
	 * @author jeff
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getListBySortInfo", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase<List<UserExtralRes>> getListBySortInfo(
			@RequestBody UserExtralReq req) {

		ResponseBase<List<UserExtralRes>> resp = new ResponseBase<List<UserExtralRes>>();

		List<UserExtralRes> respList = new ArrayList<UserExtralRes>();

		List<UserExtraInfo> infoList = new ArrayList<UserExtraInfo>();

		HqlFilter hqlFilter = new HqlFilter();

		Integer pageSize = req.getPageSize();
		Integer curPage = req.getCurPage();
		Integer sortType = req.getSortType();
		logger.info("app/userPointsCtr/getListBySortInfo sortType = "
				+ sortType + "   curPage = " + curPage + "   pageSize = "
				+ pageSize);
		if (sortType != null) {
			 
			hqlFilter = getFilter(sortType);
		 
			logger.info("heqlFilter = " + hqlFilter.getWhereAndOrderHql());
			infoList = userExtraService.findByFilter(hqlFilter, curPage,
					pageSize);

			if (infoList != null) {
				for (UserExtraInfo extraInfo : infoList) {
					UserExtralRes resInfo = new UserExtralRes();
					ReqToModelHelper.copyReqValueToModel(extraInfo, resInfo);
					SysUser user = userService.findById(extraInfo.getUserId());
					if (user != null) {
						resInfo.setUserRealName(user.getEncryptRealName());
						resInfo.setUserPhoneNum(user.getEncryptName());
					}
					respList.add(resInfo);

				}
			}
			resp.setResult(respList);
			resp.setSize(respList.size());
			resp.setResultCode(StatusCode.SUCCESS.key());
		} else {
			resp.setMsg("用户信息不存在！");
			resp.setResultCode(StatusCode.PARAM_ERROR.key());
		}

		return resp;
	}

	private HqlFilter getFilter(Integer sortType) {
		HqlFilter hqlFilter = new HqlFilter();

		if (sortType == ConstantPointRule.POINT_RECORD_SORT_01) {
			hqlFilter.addSql(" order by totalPoints desc");
		} else if (sortType == ConstantPointRule.POINT_RECORD_SORT_02) {
			hqlFilter.addSql(" order by relationUserAmount desc");
		} else if (sortType == ConstantPointRule.POINT_RECORD_SORT_03) {
			hqlFilter.addSql(" order by tieCardAmount desc");
		} else if (sortType == ConstantPointRule.POINT_RECORD_SORT_04) {
			hqlFilter.addSql(" order by bespeakOrderAmount desc");
		} else if (sortType == ConstantPointRule.POINT_RECORD_SORT_05) {
			hqlFilter.addSql(" order by payOrderAmount desc");
		}
		hqlFilter.addOrder("desc");

		return hqlFilter;
	}

	/**
	 * 根据用户姓名和手机号获取相关信息
	 * 
	 * @param userRealName
	 * @param userPhoneNum
	 * @return
	 */
	private List<UserExtraInfo> getListByUserName(HqlFilter hqlFilter,
			Integer pageSize, Integer curPage) {
		ResponseBase<List<UserExtraInfo>> resp = new ResponseBase<List<UserExtraInfo>>();
		List<UserExtraInfo> infoList = new ArrayList<UserExtraInfo>();

		List<SysUser> userList = new ArrayList<SysUser>();
		userList = userService.findByFilter(hqlFilter, curPage, pageSize);

		if (userList != null && userList.size() > 0) {
			for (SysUser user : userList) {
				UserExtraInfo extraInfo = userExtraService.findByUserId(user
						.getId());

				if (extraInfo != null) {
					infoList.add(extraInfo);
				}
			}

		}

		return infoList;
	}

 
	/**
	 * 用户获取积分接口
	 * @param userId ：用户id, ruleId : 积分规则id
	 * @return
	 */
	@RequestMapping(value = "/getPoints", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase<UserPointsMsg> getPoints(@RequestBody UserExtralReq req) {
		ResponseBase<UserPointsMsg> resp = new ResponseBase<UserPointsMsg>();

		Long userId = req.getUserId();
		Integer ruleId = req.getRuleId();
		if (userService.existById(userId) && ruleId != null) {
			UserPointsMsg userPointsMsg = PointsFunctionService.addPoints(
					userId, ruleId);
			resp.setResult(userPointsMsg);
			resp.setResultCode(StatusCode.SUCCESS.key());
		} else {
			resp.setMsg("参数错误！");
			resp.setResultCode(StatusCode.PARAM_ERROR.key());
		}
		return resp;
	}
 

}
